function initFunc(){
	$('.bt-mobile-validator-send').unbind('click').on(
			'click',
			function() {
				$(this).prop('disabled', true);
				var eid = $(this).attr('data-id');
				var val = getMobilePhoneNum(eid);
				clearFieldError(eid);
				
				$.getJSON(
						$(this).attr('data-request'),
						{
							'id' : $(this).attr('data-field'),
							'val' : val,
							'token': $(this).attr('data-key'),
							'tracker': getTrackerKey(eid),
						}
				).done(function(data) {
					if(typeof data.key !== 'undefined'){
						$('#' + eid).attr('data-key', data.key);
					}
					
					if(data.result === 'false'){
						showFieldError(data.msg, eid, true);
					}else if(data.result === 'true'){
						startCountDown(eid, data);
					}else{
						showFieldError($('#' + eid + '-send').attr('data-error-msg'), eid, true);
						resetCountDownButton(eid);
					}
				}).fail(function() {
					showFieldError($('#' + eid + '-send').attr('data-error-msg'), eid, true);
					resetCountDownButton(eid);
				}).always(function() {
					
				});
			}
	);
	
	$('.input-code-validator').unbind('keyup').on(
			'keyup',
			function() {
				var code_id = $(this).attr('id');
				var eid = $(this).attr('data-id');
				if($(this).val().length != $(this).attr('data-length')){
					return;
				}
				clearFieldError(eid);
				showLoading(eid);
				$.getJSON(
						$(this).attr('data-request'),
						{
							'id' : $(this).attr('data-field'),
							'val' : $(this).val(),
							'mobile': getMobilePhoneNum(eid),
							'tracker': getTrackerKey(eid),
						}
				).done(function(data) {
					if(data.result === 'false'){
						showFieldError(data.msg, eid, false);
					}else if(data.result === 'true'){
						confirmMobilePhone(code_id, eid, data);
					}
				}).fail(function() {
					
				}).always(function() {
					dismissLoading(eid);
				});
			}
	);
	
	$('.bt-mobile-validator-send').each(function(){
		var eid = $(this).attr('data-id');
		var key = $('#' + eid + '-send').attr('data-target-field');
		$('#Field' + key).attr('data-id', eid);
		$('#Field' + key).on('change', function(){
			resetMobileValidator($(this).attr('data-id'));
		});
	})
}

var loading = 0;

function resetMobileValidator(eid){
	var code_id = $('#' + eid + '-send').attr('data-target');	
	if(!$('#' + code_id).prop('readonly')){
		return;
	}
	initFunc();
	clearFieldError(eid);
	
	$('#' + code_id).prop('readonly', false);
	$('#' + eid + '-send').show();
	$('#' + code_id + '_confirmed').html("");
	$('#Field' + $('#' + eid + '-send').attr('data-index')).val('');
}

function getMobilePhoneNum(eid){
	var key = $('#' + eid + '-send').attr('data-target-field');
	var mobile = $('#Field' + key).val();
	if($('#AreaCodeField' + key).length > 0){
		mobile = '+' + $('#AreaCodeField' + key).val() + mobile;
	}
	return mobile;
}

function showLoading(eid){
	if(loading == 0){
		$('#' + eid + '_loading').show();
	}
	loading++;
}

function dismissLoading(eid){
	loading--;
	if(loading == 0){
		$('#' + eid + '_loading').hide();
	}
}

function confirmMobilePhone(code_id, eid, data){
	clearFieldError(eid);
	$('#' + code_id).prop('readonly', true);
	$('#' + code_id).unbind('keyup');
	$('#' + eid + '-send').hide();
	var code_input_id = $('#' + eid + '-send').attr('data-target');	
	$('#' + code_input_id + '_confirmed').html("√");
	$('#Field' + $('#' + eid + '-send').attr('data-index')).val(data.pass);
}

function getTrackerKey(eid){
	var id = $('#' + eid + '-send').attr('data-field');
	var tracker = '';
	if (typeof group_id!='undefined') {
				id = group_id[id];
		console.log("group id used: "+group_id[id]);
	}
	if (typeof globalTracker!=='undefined'){
		tracker = globalTracker;
	}
	if(getParameterByName('tracker')!=''){
		tracker = getParameterByName('tracker');
	}
	return tracker;
}

function resetCountDownButton(eid){
	$('#' + eid + '-send').prop('disabled', false);
	$('#' + eid + '-send').text($('#' + eid + '-send').attr('data-label'));
	$('#' + eid + '-send').removeClass('is-countdown');
}

function startCountDown(eid, data){
	$('.flag-container').click(false);
	$('.selected-flag').click(false);
	var time = data.resend_interval;
	var code_input_id = $('#' + eid + '-send').attr('data-target');	
	$('#' + code_input_id + '_prefix').html(data.prefix);
	$('#' + eid + '_div').show();
	
	shortly = new Date();
	shortly.setSeconds(parseInt(time) + shortly.getSeconds());
	$('#' + eid + '-send').countdown({
		until : shortly,
		format : 'S',
		layout : $('#' + eid + '-send').attr('data-count-down-template'),
		onExpiry : function() {
			resetCountDownButton(eid);
		}
	});
}

function clearFieldError(eid){
	var index = $('#' + eid + '-send').attr('data-index');
	$('#ErrMsg' + index).html('');
	$('#ErrMsg' + index).hide();
}

function showFieldError(msg, eid, reset){
	var index = $('#' + eid + '-send').attr('data-index');
	$('#ErrMsg' + index).html(msg);
	$('#ErrMsg' + index).show();
	if(reset){
		resetCountDownButton(eid);
	}
}

function checkMobileValidator(obj_id, required) {
	var isPass = true;
	var eid = $("#Field" + obj_id).attr('data-id');
	var this_val = $("#Field" + obj_id).val();
	textId = $('[name="HiddenFieldID'+obj_id+'"]').val();
	textValue = $('#Field'+obj_id).val();
	
	if(this_val.length != 32){
		showFieldError($("#Field" + obj_id).attr('data-error-msg'), eid, false);
		return false;
	}
	showLoading(eid);
	$.ajax({
		  dataType: "json",
		  url: $("#Field" + obj_id).attr('data-request'),
		  data: {
				'id' : $('#' + eid + '-send').attr('data-field'),
				'pass' : this_val,
				'mobile': getMobilePhoneNum(eid),
				'tracker': getTrackerKey(eid),
			},
		  async: false, 
			
	}).done(function(data) {
		if(data.result === 'false'){
			isPass = false;
			showFieldError(data.msg, eid, false);
		}
	}).fail(function() {
		isPass = false;
	}).always(function() {
		dismissLoading(eid);
	});
	
	return isPass;
}

$(function(){
	initFunc();
})